import time,re
#解析时间
import plugin.Remind.today_time as today_time
class todat_time_analysis():

    def Analytical_time_voice(self,voice): 
        '''解析voice时间函数 返回 依次数字 元祖类型'''
        s_j =  re.compile(r'\d+\.?\d*').findall(voice)
            #防止只有小时没有分钟
            
        try:s=int(s_j[0])
        except:s=0    
        
        try:f=int(s_j[1])
        except:f=0
        
        try:m=int(s_j[2])
        except:m=0
        #-------------------
        try:n=int(s_j[3])
        except:n=0    
        
        try:y=int(s_j[4])
        except:y=0
        
        try:r=int(s_j[5])
        except:r=0

       
        return s,f,m, n,y,r
        
    #第二判断 上午还是下午 
    def Morning_afternoon(self,voice): 

        if voice.count("上午") or voice.count("早上") or voice.count("中午"):
            return "上"
        elif voice.count("下午")or voice.count("晚上"):
            return "下"

    
    def time_stamp(self,voice):
        '''计时 时间戳函数 返回 解析出来的每个数字和 计算总共时间-单位秒  和 结束时间戳 元祖类型'''
        s,f,m,n,y,r=self.Analytical_time_voice(voice)
        
        if voice.count("小时") and voice.count("分钟") and  voice.count("秒"):
            js_time = s*3600+f*60+m
            
        elif voice.count("小时") and voice.count("分钟"):
            js_time = s*3600+f*60                
            
        elif voice.count("小时") and  voice.count("秒"):
            js_time = s*3600+f 
            
        elif voice.count("小时") :
            js_time = s*3600
                       
        elif  voice.count("分钟")and  voice.count("秒"):
            js_time = s*60+f
                          
        elif voice.count("分钟"):
            js_time = s*60
            
        elif  voice.count("秒"):
            js_time = s
        #返回 解析出来的每个数字和  计算总共时间 和 结束时间戳  
        return s,f,m,js_time,int(time.time())+js_time 

            
    #第三判断 准确时间戳
    def 明天(self,voice):
        
        s,f,m,n,y,r=self.Analytical_time_voice(voice) 
        
        if self.Morning_afternoon(voice)=='上':
                          
            timeStamp = today_time.today_time()[-1] + s*3600+f*60          
            otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timeStamp))  
            print(otherStyleTime) 
            return {1:[timeStamp,voice]}
            
        elif self.Morning_afternoon(voice)=='下':
            timeStamp = today_time.today_time()[-1] +( 12+s)*3600+f*60          
            otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timeStamp))  
            print(otherStyleTime) 
            return {1:[timeStamp,voice]}
            
    #第三判断 准确时间戳
    def 今天(self,voice):
    
        s,f,m,n,y,r=self.Analytical_time_voice(voice)
        
        if self.Morning_afternoon(voice)=='上':
 
            timeStamp = today_time.today_time()[-1] - (24-s)*3600+f*60          
            otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timeStamp))  
            print(otherStyleTime) 
            return {1:[timeStamp,voice]}
            
        elif self.Morning_afternoon(voice)=='下':

            timeStamp = today_time.today_time()[-1] - (24-12-s)*3600+f*60          
            otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timeStamp))  
            print(otherStyleTime)    
            return {1:[timeStamp,voice]}
            
        #计时   
        else:           
            print('提醒'+"-"*10)
            data = self.time_stamp(voice)
            timeStamp = data[-1]    
            otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timeStamp))  
            print(self.time_stamp(voice),otherStyleTime) 
            return {1:[timeStamp,voice]}
            
    #第三判断 准确时间戳
    def 每天(self,voice):
    
    
        s,f,m,n,y,r=self.Analytical_time_voice(voice)
        
        if self.Morning_afternoon(voice)=='上':
           
            timeStamp = today_time.today_time()[-1] - (24-s)*3600+f*60          
            otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timeStamp))  
            print(otherStyleTime) 
            return {0:[s,f,m,voice]}
            
        elif self.Morning_afternoon(voice)=='下':
        
            timeStamp = today_time.today_time()[-1] - (24-12-s)*3600+f*60          
            s = time.strftime("%H", time.localtime(timeStamp)) 
            f = time.strftime("%M", time.localtime(timeStamp))
            m = time.strftime("%S", time.localtime(timeStamp)) 
            print("每天",s,f,m)   
            return {0:[s,f,m,voice]}
    
    # #第四判断 准确日期
    # def 月(self,voice):
        
    #     #2020年4月16号早上6点
    #     #获取数字2020,4,16,6
    #     #获取数字2020,4,16,18
    #     n,y,r,s,f,m=self.Analytical_time_voice(voice)
    #     #12小时制分上午和下午
    #     if self.Morning_afternoon(voice)=='上':
    #         return {2:[n,y,r,s,f,m,voice]}

    #     elif self.Morning_afternoon(voice)=='下':
    #         return {2:[n,y,r,s+12,f,m,voice]}
    
    def main(self,voice):
    
        '''第一判断 今天 -明天- 每天'''     
        if voice.count("明天"):
            return self.明天(voice)
        elif voice.count("每天"):
            return self.每天(voice)
        #elif voice.count("月"):
        #    return self.月(voice) 
        else:
            return self.今天(voice)